.code32
.text
.globl startup_32, idt, gdt, pg_dir, tmp_floppy_area
pg_dir:
startup_32:
    movl $0x10, %eax
    movw %ax,   %ds
    #movw %ax,   %es
    #movw %ax,   %fs
    #movw %ax,   %gs

    lss stack_start, %esp
    call setup_idt
    call setup_gdt
    #movl $7000, %ecx
    #call dsply0

    call set_clock_idt
    #movl $0x10, %eax
    #movw %ax,   %ds
    #movw %ax,   %es
    #movw %ax,   %fs
    #movw %ax,   %gs
    #lss stack_start, %esp
    #int $0x80
    #movl  $0x18, %eax
    #movw  %ax, %gs
    #movl  $0x80, %edi
    #movb  $'K', %al
    movb  $0x0c, %ah
    movw  %ax,   %gs:(%edi)

#    movl $0x18, %eax
#    movw %ax, %gs
#    movl $0x0, %edi
#    movb $0xf, %ah
    #movb $0x42, %al
#    movb $0x43, %al
#    movw %ax, %gs:(%edi)

    movl $0x10, %eax
    movw %ax,   %ds
    movw %ax,   %es
    movw %ax,   %fs

    movl $0x18, %eax
    movw %ax, %gs

    sti

#    movl $0x0, %edi
#    movb $0xf, %ah
#    movb $0x42, %al
#    movw %ax, %gs:(%edi)

    xorl %eax, %eax
1:
    incl %eax
    movl %eax, 0x000000
    cmpl %eax, 0x100000
    je   1b


    jmp  after_page_tables


#loop:
#    jmp loop

setup_idt:
    leal ignore_int, %edx
    movl $0x00080000, %eax
    movw %dx, %ax
    movw $0x8e00, %dx
    leal idt, %edi
    movl $256, %ecx
rp_sidt:
    movl %eax, (%edi)
    movl %edx, 4(%edi)
    addl $8, %edi
    decl %ecx
    jne  rp_sidt
    lidt idt_descr
    ret

setup_gdt:
    lgdt gdt_descr
    ret

.org 0x1000
pg0:

.org 0x2000
pg1:

.org 0x3000
pg2:

.org 0x4000
pg3:

.org 0x5000

#PG4:

#.org 0x6000
tmp_floppy_area:
.fill 1024, 1, 0

after_page_tables:
/*we call jump to main at this*/
    movl  $0x18, %eax
    movw  %ax, %gs
    movl  $0x80, %edi
    movb  $'K', %al
    movb  $0x0c, %ah
    movw  %ax,   %gs:(%edi)
    pushl $0
    pushl $0
    pushl $0
    pushl $L6
    pushl $main
    movl  $0x18, %eax
    movw  %ax, %gs
    movl  $0x80, %edi
    movb  $'K', %al
    movb  $0x0c, %ah
    movw  %ax,   %gs:(%edi)

    jmp setup_paging
L6:
    jmp L6


ignore_int:
    /* we do not have function _printk now, so trick it */
    pushl %eax
    pushl %ecx
    pushl %edx
    pushw %ds
    pushw %es
    pushw %fs
    movl  $0x10, %eax
    movw  %ax, %ds
    movw  %ax, %es
    movw  %ax, %fs
    /* call  _printk */
    #movl  $0x96, %edi
    movl  $0x90, %edi
    movb  $'I', %al
    movb  $0x0c, %ah
    movw  %ax,   %gs:(%edi)
    popw  %fs
    popw  %es
    popw  %ds
    popl  %edx
    popl  %ecx
    popl  %eax
    iret

set_clock_idt:
    leal clock_handle, %edx
    movl $0x0080000, %eax
    movw %dx, %ax
    movw $0x8e00, %dx
    leal idt, %edi
    addl $0x100, %edi
    movl %eax, (%edi)
    movl %edx, 4(%edi)
    ret
clock_handle:
    movl $0x96, %edi
    incb %gs:(%edi)
    /* 向0x20端口发送EOI */
    movb $0x20, %al
    outb %al, $0x20
    iret

.align 4
setup_paging:
    movl $1024*5, %ecx
    xorl %eax,     %eax
    xorl %edi,     %edi
    cld
    rep
    stosl

    movl $pg0 + 7, pg_dir
    movl $pg1 + 7, pg_dir + 4
    movl $pg2 + 7, pg_dir + 8
    movl $pg3 + 7, pg_dir + 12
    #movl $pg4 + 7, pg_dir + 16
    movl $pg3 + 4092, %edi
    movl $0xfff007, %eax
    std
1:
    stosl
    subl $0x1000, %eax
    jge  1b

#    movl $pg4 + 4092, %edi #我增加的代码
#    movl $0xfd3ff007, %eax
#    std
#2:
#    stosl
#    subl $0x1000, %eax
#    cmpl $0xfd000007, %eax
#    jge  2b

    xorl %eax, %eax
    movl %eax, %cr3
    movl %cr0, %eax
    orl  $0x80000000, %eax
    #call dsply0 #这里画像素不出错，视频地址：0xFD000000
    movl $0, %ecx
    call dsply0 #这里画像素出错！视频地址：0xFD000000
    movl %eax, %cr0
    #movl $0, %ecx
    #call dsply0 #这里画像素出错！视频地址：0xFD000000
   

    ret

dsply0:
    movl $0x20, %eax
    movw %ax, %gs
    movl %ecx, %edi
       movb $15, %al
       movb %al, %gs:(%edi)
    ret

#    movl $0xfcf70000, %eax
#    #movl $0xfd000000, %eax
#    movb $15, (%eax)
#    movl $1024, %ecx
#    imull $768, %ecx
#    subl $1, %ecx
#    addl %ecx, %eax
#    movb $15, (%eax)
#    subl $300000, %eax
#    movl $0, %ecx
#loop_start:
#    cmp $100, %ecx        # 比较计数器和 10
#    je loop_end          # 如果计数器等于 10，跳转到循环结束
#    movb $15, (%eax)
#    incl %ecx            # 计数器加 1
#    incl %eax
#    jmp loop_start       # 跳转到循环开始

#loop_end:
#     ret


.align 4
.word 0
idt_descr:
    .word 256*8-1
    .long idt

.align 4
.word 0
gdt_descr:
    .word 256*8-1
    .long gdt

.align 8
idt:
    .fill 256, 8, 0

gdt:
    .quad 0x0000000000000000
    .quad 0x00c09a0000000fff
    .quad 0x00c0920000000fff
    .quad 0x00c0f20b8000ffff
    #.quad 0xfdcc920000000000  #level 0
    .quad 0xfdccf20000000000   #level 3
    .quad 0x0000000000000000
    .fill 251, 8,0
